package com.csjbot.alicecontrolserver;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ScrollView;
import android.widget.TextView;

import com.csjbot.alicecontrolserver.Events.ServerEvent;
import com.csjbot.alicecontrolserver.tool.ShellUtils;
import com.orhanobut.logger.Logger;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class MainActivity extends AppCompatActivity {

    private Intent serverIntent;
    private TextView connectTextView, showInfoTextView;
    private EventBus eventBus;
    private ScrollView scrollView;

    /**
     * 得到无线网关的IP地址
     *
     * @return
     */
    private String getWirelseeIp() {
        try {
            // 获取本地设备的所有网络接口
            Enumeration<NetworkInterface> enumerationNi = NetworkInterface
                    .getNetworkInterfaces();
            while (enumerationNi.hasMoreElements()) {
                NetworkInterface networkInterface = enumerationNi.nextElement();
                String interfaceName = networkInterface.getDisplayName();
                Log.i("tag", "网络名字" + interfaceName);

                // 如果是无线网卡
                if (interfaceName.equals("eth0")) {
                    Enumeration<InetAddress> enumIpAddr = networkInterface
                            .getInetAddresses();

                    while (enumIpAddr.hasMoreElements()) {
                        // 返回枚举集合中的下一个IP地址信息
                        InetAddress inetAddress = enumIpAddr.nextElement();
                        // 不是回环地址，并且是ipv4的地址
                        if (!inetAddress.isLoopbackAddress()
                                && inetAddress instanceof Inet4Address) {
                            Logger.d(inetAddress.getHostAddress() + "   ");

                            return inetAddress.getHostAddress();
                        }
                    }
                }
            }

        } catch (SocketException e) {
            e.printStackTrace();
        }
        return "";

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        serverIntent = new Intent(this, ServerService.class);
        startService(serverIntent);

        connectTextView = (TextView) findViewById(R.id.connectTextView);
        showInfoTextView = (TextView) findViewById(R.id.showInfoTextView);
        scrollView = (ScrollView) findViewById(R.id.scrollView);

        TextView ipaddr = (TextView) findViewById(R.id.tv_ipaddr);
        ipaddr.setText(getWirelseeIp());

        if (eventBus == null) {
            eventBus = EventBus.getDefault();
        }
        eventBus.register(this);

        //ShellUtils.execCommand("ifconfig eth0 up", true);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void messageRecAndShow(ServerEvent event) {
        if (event.getTag() == ServerEvent.CLIENT_CONNECTED) {
            connectTextView.setText("客户端已经连接 " + event.getMegString());
        }
        if (event.getTag() == ServerEvent.CLIENT_DISCONNECTED) {
            connectTextView.setText("等待连接");
        }

        if (event.getTag() == ServerEvent.MESSAGE_COMMING) {
            Logger.d(event.getMegString());
            showInfoTextView.append(event.getMegString() + "\r\n");
            scrollView.fullScroll(ScrollView.FOCUS_DOWN);//滚动到底部
        }
    }
}
